package cn.iocoder.yudao.framework.business.basic.service.impl;

import cn.iocoder.yudao.framework.business.basic.pojo.dto.QueryDTO;
import cn.iocoder.yudao.framework.business.basic.pojo.po.MerchantScopeEntity;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.MerchantScopeVO;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.ShopSimpleVO;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import jakarta.annotation.Nonnull;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  服务接口
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/11/29
 */
public abstract class MerchantScopeService<PO extends MerchantScopeEntity, VO extends MerchantScopeVO, DTO extends QueryDTO> extends BaseService<PO, VO, DTO> {

    @Override
    public List<VO> convertToViewObject(@Nonnull List<PO> entities) {
        List<VO> list = convertList(entities, this::convertToViewObject);
        Set<Long> shopIds = convertSet(entities, PO::getShopId);
        Map<Long, ShopSimpleVO> shops = getShopSimpleMap(shopIds);
        for (VO vo : list) {
            vo.setShop(shops.getOrDefault(vo.getShopId(), ShopSimpleVO.EMPTY));
            RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, vo.getShop().getCountyCode(), vo.getShop());
        }
        return list;
    }

    @DataPermission(enable = false)
    @Nonnull
    protected abstract Map<Long, ShopSimpleVO> getShopSimpleMap(Collection<Long> shopIds);

}
